//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.indexes.customerlist;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.DataModes;
import com.nebutown.commonunits.dataface.DateTypes;
import com.nebutown.commonunits.dataface.EntryTypes;
import com.nebutown.commonunits.dataface.FormShell;
import com.nebutown.commonunits.dataface.TableShell;
import com.nebutown.commonunits.dataface.definitions.ButtonField;
import com.nebutown.commonunits.dataface.definitions.FieldValue;
import com.nebutown.commonunits.dataface.definitions.PageInfo;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.description.data.SortSpec;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.customer.Customer;
import com.restaurant.application.customer.support.CustomerData;
import com.restaurant.application.indexes.customerlist.mysql.CustomerListBean;
import com.restaurant.application.indexes.customerlist.mysql.CustomerListCriteria;
import com.restaurant.application.indexes.customerlist.mysql.CustomerListImpl;
import com.restaurant.application.indexes.customerlist.support.AbstractAssistCustomerList;
import com.restaurant.application.indexes.customerlist.support.SearchData;
import com.restaurant.application.util.CommonUtils;
import java.util.List;
import java.util.Objects;

public class AssistCustomerListImpl extends AbstractAssistCustomerList {
    public AssistCustomerListImpl() {
    }

    public View getFullView() {
        return this.newCommonTable(new SearchData());
    }

    public View getViewLead(ViewSpec viewSpec) {
        return this.newCommonTable(new SearchData());
    }

    public ViewUpdate search(SearchData search, int[] seq) {
        return this.returnView(new View[]{this.newCommonTable(search)});
    }

    private String getWhere(SearchData search) {
        StringBuilder builder = new StringBuilder();
        builder.append(" true");
        if (!Strings.isNullOrEmpty(search.getPhone())) {
            builder.append(" and PHONE like '%").append(search.getPhone()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getName())) {
            builder.append(" and NICKNAME like '%").append(search.getName()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getNumber())) {
            builder.append(" and NUMBER like '%").append(search.getNumber()).append("%'");
        }

        if (!Strings.isNullOrEmpty(search.getLevelName())) {
            builder.append(" and LEVELNAME = '").append(search.getLevelName()).append("'");
        }

        if (Objects.nonNull(search.getCreateTime())) {
            if (Objects.nonNull(search.getCreateTime().getFrom()) && search.getCreateTime().getFrom() > 0L) {
                builder.append(" and CREATETIME >=").append(search.getCreateTime().getFrom());
            }

            if (Objects.nonNull(search.getCreateTime().getTo()) && search.getCreateTime().getTo() > 0L) {
                builder.append(" and CREATETIME <").append(search.getCreateTime().getTo());
            }
        }

        if (!Strings.isNullOrEmpty(search.getState())) {
            builder.append(" and STATE = '").append(search.getState()).append("'");
        }

        builder.append("  ORDER BY MODIFYTIME DESC, RECORD_ID ASC");
        return builder.toString();
    }

    public ViewUpdate modify(int[] seq) {
        List<CustomerListBean> beans = ((CustomerListImpl)this.getIndex(CustomerListImpl.class)).search((new CustomerListCriteria()).withRecordId((long)seq[0]), (SortSpec)null, 1);
        return beans.size() > 0 ? this.returnPushInContainer(((CustomerListBean)beans.get(0)).getCustomerUid(), false) : this.returnView(new View[]{this.getFullView()});
    }

    public ViewUpdate modifyScores(int[] seq) {
        List<CustomerListBean> beans = ((CustomerListImpl)this.getIndex(CustomerListImpl.class)).search((new CustomerListCriteria()).withRecordId((long)seq[0]), (SortSpec)null, 1);
        return beans.size() > 0 ? this.returnView(new View[]{((Customer)this.getUnit(Customer.class, ((CustomerListBean)beans.get(0)).getCustomerUid())).modifyScores(this.unitPosition())}) : this.returnView(new View[]{this.getFullView()});
    }

    private View newCommonTable(SearchData search) {
        int page = 1;
        if (Objects.nonNull(search.getPage()) && search.getPage() > 1) {
            page = search.getPage();
        }

        String where = this.getWhere(search);
        List<CustomerListBean> beans = ((CustomerListImpl)this.getIndex(CustomerListImpl.class)).search(where, (page - 1) * 10, page * 10);
        int totalRecord = ((CustomerListImpl)this.getIndex(CustomerListImpl.class)).count(where);
        PageInfo info = CommonUtils.getPageInfo(page, 10, totalRecord, this, CMD_SEARCH, "page");
        return ((UIDataFace)this.getSingleton(UIDataFace.class)).newUniForm(this.unitPosition(), this.newFormShell(search, beans, info).getForm());
    }

    private FormShell newFormShell(SearchData search, List<CustomerListBean> beans, PageInfo info) {
        FormShell shell = new FormShell("会员管理/会员信息", 3, 4);
        shell.source(search, DataModes.EDIT);
        shell.at(0, 0, "手机号").stringField("phone", search.getPhone()).placeHolder("手机号").setEditable(true);
        shell.at(0, 1, "姓名").stringField("name", search.getName()).placeHolder("姓名").setEditable(true);
        shell.at(0, 2, "会员编号").stringField("number", search.getNumber()).placeHolder("会员编号").setEditable(true);
        shell.at(1, 0, "会员等级").selectEntry("levelName", search.getLevelName(), CommonUtils.getLevelNameList(this)).placeHolder("会员等级").setEditable(true);
        shell.at(1, 1, "创建时间").intervalField("createTime", search.getCreateTime(), DateTypes.DATETIME).notNull().setEditable(true);
        shell.at(2, 0, "状态").selectEntry("state", (String)null, "", new String[]{"正常", "黑名单用户"}).placeHolder("状态").setEditable(true);
        shell.at(3, 0, (String)null).buttonBoxField(new ButtonField[]{new ButtonField(this.getPath(CMD_SEARCH, new int[0]), "查询", true, "", false)});
        TableShell<CustomerListBean> tableShell = new TableShell(CustomerListBean.class, beans);
        tableShell.createColumn("number").withName("会员编号").withWidth(110).cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getNumber());
        });
        tableShell.createColumn("nickName").withName("姓名").cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getNickName());
        });
        tableShell.createColumn("levelName").withName("等级").withWidth(60).cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getLevelName());
        });
        tableShell.createColumn("usableScores").withName("可用积分").withWidth(90).cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getUsableScores());
        });
        tableShell.createColumn("gender").withName("性别").withWidth(60).cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getGender());
        });
        tableShell.createColumn("createTime").withName("创建时间").cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(DateTimeUtils.toTime(bean.getCreateTime()));
        });
        tableShell.createColumn("phone").withName("手机号").cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getPhone());
        });
        tableShell.createColumn("state").withName("用户状态").withWidth(100).cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            return (new FieldValue()).withValue(bean.getState());
        });
        tableShell.createColumn("name").withName("操作").withWidth(220).cellCalculator((e) -> {
            CustomerListBean bean = (CustomerListBean)e;
            List<ButtonField> fields = Lists.newArrayList();
            if (!Strings.isNullOrEmpty(this.getPath(CMD_MODIFY, new int[0]))) {
                fields.add((new ButtonField()).withLabel("编辑").withWithData(false).withButtonPath(this.getPath(CMD_MODIFY, new long[]{bean.getRecordId()})));
            }

            if (!Strings.isNullOrEmpty(this.getPath(CMD_MODIFYSCORES, new int[0]))) {
                fields.add((new ButtonField()).withLabel("修改积分").withWithData(false).withButtonPath(this.getPath(CMD_MODIFYSCORES, new long[]{bean.getRecordId()})));
            }

            return fields.size() > 0 ? (new FieldValue()).withButtonBox(fields).withEntryType(EntryTypes.BUTTONBOX) : (new FieldValue()).withValue("");
        });
        tableShell.withPageInfo(info);
        shell.at(4, 0, "会员管理/会员信息").large(3, 1).tableField("customerList", tableShell);
        return shell;
    }

    public CustomerListBean createBean(UID uid, CustomerData data) {
        return !Objects.isNull(uid) && !Objects.isNull(data) ? (new CustomerListBean()).withCustomerUid(uid).withId(data.getId()).withCreateTime(data.getCreateTime()).withModifyTime(data.getModifyTime()).withAvatarUrl(data.getAvatarUrl()).withNickName(data.getNickName()).withRealName(data.getRealName()).withGender(data.getGender()).withBirthday(data.getBirthday()).withOpenId(data.getOpenId()).withPhone(data.getPhone()).withNumber(data.getNumber()).withQrCodeUrl(data.getQrCodeUrl()).withLevelUid(data.getLevelUid()).withLevel(data.getLevel()).withLevelName(data.getLevelName()).withUsableScores(String.valueOf(data.getUsableScores())).withTotalScores(String.valueOf(data.getTotalScores())).withState(data.getState()).withQrCodeUid(data.getQrCodeUid().value()) : null;
    }

    public CustomerListBean updateBean(UID uid, CustomerData data, CustomerListBean bean) {
        if (!Objects.isNull(uid) && !Objects.isNull(data) && !Objects.isNull(bean)) {
            bean.withCustomerUid(uid);
            if (Objects.nonNull(data.getModifyTime())) {
                bean.withModifyTime(data.getModifyTime());
            }

            if (!Strings.isNullOrEmpty(data.getAvatarUrl())) {
                bean.withAvatarUrl(data.getAvatarUrl());
            }

            if (!Strings.isNullOrEmpty(data.getNickName())) {
                bean.withNickName(data.getNickName());
            }

            if (!Strings.isNullOrEmpty(data.getRealName())) {
                bean.withRealName(data.getRealName());
            }

            if (!Strings.isNullOrEmpty(data.getGender())) {
                bean.withGender(data.getGender());
            }

            if (!Strings.isNullOrEmpty(data.getBirthday())) {
                bean.withBirthday(data.getBirthday());
            }

            if (!Strings.isNullOrEmpty(data.getOpenId())) {
                bean.withOpenId(data.getOpenId());
            }

            if (!Strings.isNullOrEmpty(data.getPhone())) {
                bean.withPhone(data.getPhone());
            }

            if (!Strings.isNullOrEmpty(data.getQrCodeUrl())) {
                bean.withQrCodeUrl(data.getQrCodeUrl());
            }

            if (Objects.nonNull(data.getUsableScores()) && data.getUsableScores() > 0L) {
                bean.withUsableScores(String.valueOf(data.getUsableScores()));
            }

            if (Objects.nonNull(data.getTotalScores()) && data.getTotalScores() > 0L) {
                bean.withTotalScores(String.valueOf(data.getTotalScores()));
            }

            if (!Strings.isNullOrEmpty(data.getState())) {
                bean.withState(data.getState());
            }

            if (Objects.nonNull(data.getLevelUid())) {
                bean.withLevelUid(data.getLevelUid());
            }

            if (!Strings.isNullOrEmpty(data.getLevel())) {
                bean.withLevel(data.getLevel());
            }

            if (!Strings.isNullOrEmpty(data.getLevelName())) {
                bean.withLevelName(data.getLevelName());
            }

            return bean;
        } else {
            return null;
        }
    }

    public UID getCustomerUidByOpenId(String openId) {
        if (!Strings.isNullOrEmpty(openId)) {
            List<CustomerListBean> beans = ((CustomerListImpl)this.getIndex(CustomerListImpl.class)).search((new CustomerListCriteria()).withOpenIdEquals(openId), (SortSpec)null, 1);
            if (beans.size() > 0) {
                return ((CustomerListBean)beans.get(0)).getCustomerUid();
            }
        }

        return null;
    }
}
